﻿//Copyright 2012 - Jonathan Allen

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Granite.Testing
{
    public class AssertionContext
    {


        readonly List<Assertion> m_Assertions = new List<Assertion>();

        public void Inconclusive(string message)
        {
            Assertions.Add(new Assertion(message, true, Environment.StackTrace));
        }

        public void EndCapture()
        {
            GC.SuppressFinalize(this);

            if (Assertions.Count == 0)
                return;

            if (Assertions.Count == 1)
            {
                if (Assertions[0].Inconclusive)
                {
                    var exception = new AssertInconclusiveException(Assertions[0].Message);
                    exception.Data["Assertions"] = Assertions;
                    throw exception;
                }
                else
                {
                    var exception = new AssertFailedException(Assertions[0].Message);
                    exception.Data["Assertions"] = Assertions;
                    throw exception;
                }
            }

            if (Assertions.All(f => f.Inconclusive))
            {
                var message = string.Format("There are {0} inconclusive assertions.", Assertions.Count);
                foreach (var item in Assertions)
                {
                    message += Environment.NewLine + item.Message;
                }
                var exception = new AssertInconclusiveException(message);
                exception.Data["Assertions"] = Assertions;
                throw exception;
            }

            {
                var failed = Assertions.Count(f => !f.Inconclusive);
                var inconclusive = Assertions.Count(f => f.Inconclusive);

                var message = string.Format("There are {0} failed assertions and {1} inconclusive assertions.", failed, inconclusive);
                foreach (var item in Assertions)
                {
                    message += Environment.NewLine + item.Message;
                }
                var exception = new AssertFailedException(message);
                exception.Data["Assertions"] = Assertions;
                throw exception;
            }
        }


        internal void IsNotNull(object value, string message)
        {
            if (value == null)
                Assertions.Add(new Assertion(message, false, Environment.StackTrace));
        }

        internal void IsTrue(bool condition, string messageFormat, params object[] parameters)
        {
            if (!condition)
                Assertions.Add(new Assertion(string.Format(messageFormat, parameters), false, Environment.StackTrace));
        }

        internal void IsNotNullOrWhiteSpace(string expression, string messageFormat, params object[] parameters)
        {
            if (string.IsNullOrWhiteSpace(expression))
                Assertions.Add(new Assertion(string.Format(messageFormat, parameters), false, Environment.StackTrace));
        }

        internal void IsNullOrWhiteSpace(string expression, string messageFormat, params object[] parameters)
        {
            if (!string.IsNullOrWhiteSpace(expression))
                Assertions.Add(new Assertion(string.Format(messageFormat, parameters), false, Environment.StackTrace));
        }

        internal void IsFalse(bool condition, string messageFormat, params object[] parameters)
        {
            if (condition)
                Assertions.Add(new Assertion(string.Format(messageFormat, parameters), false, Environment.StackTrace));
        }
        public List<Assertion> Assertions
        {
            get { return m_Assertions; }
        }
    }

}
